﻿using Learn.Repository.Interface;
using Learn.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Learn.Service.EFCore
{
    /// <summary>
    /// 基础EF扩展实现类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Base_EFCore<T> : I_Base_EFCore<T> where T : class, new()
    {
        private readonly I_Repository<T> _repository;

        public Base_EFCore(I_Repository<T> repository)
        {
            _repository = repository;
        }

        public void Add(T t)
        {
           _repository.Add(t);
        }

        public void AddRange(IEnumerable<T> ie)
        {
            _repository.AddRange(ie);
        }

        public void Delete(T t)
        {
           _repository.Delete(t);
        }

        public void DeleteRange(IEnumerable<T> ie)
        {

            _repository.DeleteRange(ie);
        }

        public T Find(params object[] keys)
        {
            return _repository.Find(keys);
        }
        /// <summary>
        /// 查询所有
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>> where = null)
        {
            return _repository.QueryAll(where);
        }

        public IQueryable<T> QueryAll<type>(bool isAsc, Expression<Func<T, type>> order, Expression<Func<T, bool>> where = null)
        {
            return _repository.QueryAll(isAsc, order, where);
        }

        public IQueryable<T> QueryAll<type>(out int total, int page, int limit, bool isAsc, Expression<Func<T, type>> order, Expression<Func<T, bool>> where = null)
        {
            return _repository.QueryAll(out total, (page - 1) * limit, limit, isAsc, order, where);
        }


        public int SaveChanges()
        {
            return _repository.SaveChanges();
        }

        public void update(T t)
        {
            _repository.update(t);
        }

        public void updateRange(IEnumerable<T> ie)
        {
            _repository.updateRange(ie);
        }
    }
}
